package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.service.TokenService;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.LoginInfo;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.SysOrganizationDTO;
import com.jcfk.system.domain.dto.SysPermissionDTO;
import com.jcfk.system.service.LoginUserPowerService;
import com.jcfk.system.service.RefUserOrganizationMappingService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录用户权限 api
 *
 * @author 李炳海
 */
@Api(tags = "loginUserPower", description = "登录用户权限")
@RestController
@RequestMapping("/loginUserPower")
public class LoginUserPowerController {

    /**
     * token服务
     */
    @Autowired
    private TokenService tokenService;

    @Autowired
    private LoginUserPowerService loginUserPowerService;

    @Autowired
    private RefUserOrganizationMappingService refUserOrganizationMappingService;

    /**
     * 获取登录用户权限菜单树
     *
     * @param applicationType 应用类型
     * @return RetResult
     */
    @PostMapping("topMenu")
    public RetResult<List<SysPermissionDTO>> getTopMenu(@RequestParam(required = false) String applicationType,@RequestParam(required = false) String parentMenuId) {
        if(StrUtil.isEmpty(parentMenuId)){
            parentMenuId="0";
        }
        return RetResult.ok(loginUserPowerService.getUserPimss(applicationType,parentMenuId));
    }

    /**
     * 获取登录用户权限菜单树
     *
     * @param applicationType 应用类型
     * @return RetResult
     */
    @PostMapping("tree")
    public RetResult<List<SysPermissionDTO>> getMenuTree(@RequestParam(required = false) String applicationType,@RequestParam(required = false) String parentMenuId) {
        List<SysPermissionDTO> list=loginUserPowerService.getUserPimss(applicationType,"");

        if(applicationType.equals("app")){
            LoginInfo loginInfo =tokenService.getLoginInfo();

            String appMenuId=loginInfo.getParameters().get("appMenuId");
            parentMenuId=appMenuId;

            //app:mes 生产 app:wms 仓储  app:eam 资产
//
//            if(StrUtil.isNotEmpty(appMenuId)){
//                if(appMenuId.equals("DKIA-MES")){
//                    parentMenuId="app:mes";
//                }
//                if(appMenuId.equals("DKIA-WMS")){
//                    parentMenuId="app:wms";
//                }
//            }
        }

        if(StrUtil.isEmpty(parentMenuId)){
            return RetResult.ok(list);
        }


        if(list.size()>0) {
            for (SysPermissionDTO dto :list){
                if(dto.getPermissionCode().equals(parentMenuId)||dto.getId().equals(parentMenuId)){
                    return RetResult.ok(dto.getChildren());
                }
            }
        }

        String pmId=parentMenuId;
        for (SysPermissionDTO dto :list){
            list=dto.getChildren().stream().filter(p->p.getPermissionCode().equals(pmId)||p.getId().equals(pmId)).collect(Collectors.toList());
            if(list.size()>0) {
                if(applicationType.equals("app")){
                    return RetResult.ok(list);
                }
                else
                {
                    return RetResult.ok(list.get(0).getChildren());
                }
            }
        }
        return RetResult.ok(new ArrayList<>());
    }

    /**
     * 获取登录用户权限列表
     *
     * @param applicationType 应用类型
     * @return RetResult
     */
    @PostMapping("codes")
    public RetResult<String[]> getPowers(@RequestParam(required = false) String applicationType) {
        return RetResult.ok(loginUserPowerService.queryPimssCode(applicationType));
    }

    /**
     * 过滤登录用户权限列表
     * @param query
     * @return
     */
    @PostMapping("filterPermission")
    public RetResult<List<SysPermissionDTO>> getUserPermissionList(@RequestBody SysPermissionDTO query){
        return RetResult.ok(loginUserPowerService.filterUserPermissionList(query.getCodes()));
    }

    /**
     * 获取组织机构列表
     * @return
     */
    @PostMapping("listOrganization")
    public RetResult<List<SysOrganizationDTO>> listOrganization(@RequestParam(required = false) String organizationType){
        return RetResult.ok(refUserOrganizationMappingService.getUserOrganList(SecurityUtils.getUserId(),organizationType));
    }
}
